// pages/goods_list/index.js
import {
  request
} from "../../request/index"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: [{
        id: 0,
        title: "综合",
      }, {
        id: 1,
        title: "销量",
      },
      {
        id: 2,
        title: "价格",
      }
    ],
    activeIndex: 0,
    goods_list: []
  },
  QueryParams: {
    query: "",
    cid: "",
    pagenum: 1,
    pagesize: 10
  },
  totalPages: 1,
  /**
   * 生命周期函数--监听页面加载
   */
  success(event) {
    console.log(event.detail.index);
    const activeIndex = event.detail.index
    this.setData({
      activeIndex
    })
  },
  onReachBottom() {
    if (this.QueryParams.pagenum >= this.totalPages) {
      wx.showToast({
        title: '没有下一页数据'
      })
      console.log(this.QueryParams.pagenum);
      console.log('没有下一页数据');
    } else {
      this.QueryParams.pagenum++;
      this.getGoodsList()
    }
    //关闭下拉刷新效果

  },
  async getGoodsList() {
    const res = await request({
      url: "/goods/search",
      data: this.QueryParams
    })
    const total = res.data.message.total;
    this.totalPages = Math.ceil(total / this.QueryParams.pagesize)
    // console.log(this.totalPages);
    console.log(this.QueryParams.pagenum);
    console.log(this.totalPages);
    // console.log(res.data.message.goods);
    this.setData({
      goods_list: [...this.data.goods_list, ...res.data.message.goods]
    })
    wx.stopPullDownRefresh()
  },
  onPullDownRefresh() {
    this.setData({
      goods_list:[]
    }),
    this.QueryParams.pagenum=1;
    this.getGoodsList()
  },
  onLoad: function (options) {
    this.QueryParams.cid = options.cid,
      this.getGoodsList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  // onPullDownRefresh: function () {

  // },

  /**
   * 页面上拉触底事件的处理函数
   */
  // onReachBottom: function () {

  // },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})